limsExperimentalTemplate.editPage = {

    getGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-experimental-template-edit-list-grid',
            url : limsExperimentalTemplate.baseUrl + '/queries/raw',
            service : 'limsExperimentalTemplateServiceImpl',
            dbTable : 'T_LIMS_EXPERIMENTAL_TEMPLATE',
            columns : limsExperimentalTemplate.getEditListColumns(),
            deleteFormatter: function (row) {
                return row.etCode || row.id;
            },
            page : true,
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData([ 'template-test-method-edit-list-grid', 'lims-experimental-template-test-edit-list-grid' ]);
                }
            },
            onRowActive : function(index, row) {
                Gikam.getComp('lims-experimental-template-test-edit-list-grid').refresh({
                    url: limsExperimentalTemplate.etTestUrl + '/queries',
                    requestData: {
                        etId_EQ: row.id
                    }
                });
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    Gikam.preInsert({
                        modalTitle : 'GIKAM.BUTTON.INSERT',
                        fields : limsExperimentalTemplate.getBaseInfoFormFields(),
                        url : limsExperimentalTemplate.baseUrl
                    }).done(function(id) {
                        Gikam.getComp('lims-experimental-template-edit-list-grid').refresh();
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('lims-experimental-template-edit-list-grid').deleteRows(limsExperimentalTemplate.baseUrl);
                }
            } ]
        }
    },

    getEtTestGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-experimental-template-test-edit-list-grid',
            service : 'limsExperimentalTemplateTestServiceImpl',
            dbTable : 'T_LIMS_EXPERIMENTAL_TEMPLATE_TEST',
            order : {'orderNo':'asc'},
            columns : limsExperimentalTemplate.getEtTestListColumns(),
            deleteFormatter: function (row) {
                return row.testCode || row.id;
            },
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData([ 'template-test-method-edit-list-grid' ]);
                }
            },
            onRowActive : function(index, row) {
                Gikam.getComp('template-test-method-edit-list-grid').refresh({
                    url: limsExperimentalTemplate.etTestMethodUrl + '/queries',
                    requestData: {
                        etTestId_EQ: row.id
                    }
                });
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    var activedRow = Gikam.getComp('lims-experimental-template-edit-list-grid').getActivedRow();
                    if (Gikam.isEmpty(activedRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    Gikam.create('modal', {
                        title: Gikam.choose.getConfig("test").title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig("test-multi").url,
                        width: '80%',
                        height: 600,
                        onAfterClose: function (rows) {
                            if (!Gikam.isEmpty(rows)) {
                                var chooseData = rows.map(function (item) {
                                    return {
                                        etId: activedRow.id,
                                        etCode: activedRow.etCode,
                                        testId: item.id,
                                        testCode: item.testCode,
                                        testName: item.testName
                                    }
                                });
                                Gikam.getComp('lims-experimental-template-test-edit-list-grid').insert(limsExperimentalTemplate.etTestUrl, chooseData).done(function () {
                                    Gikam.getComp('lims-experimental-template-test-edit-list-grid').refresh();
                                });
                            }
                        }
                    })
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('lims-experimental-template-test-edit-list-grid').deleteRows(limsExperimentalTemplate.etTestUrl);
                }
            } ]
        }
    },

    getSpMethodGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'template-test-method-edit-list-grid',
            service : 'limsExperimentalTemplateTestMethodServiceImpl',
            dbTable : 'T_LIMS_EXPERIMENTAL_TEMPLATE_TEST_METHOD',
            columns : limsExperimentalTemplate.getEtMethodListColumns(),
            deleteFormatter: function (row) {
                return row.methodCode || row.id;
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    var activedRow = Gikam.getComp('lims-experimental-template-test-edit-list-grid').getActivedRow();
                    if (Gikam.isEmpty(activedRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    Gikam.create('modal', {
                        title: Gikam.choose.getConfig("test-method").title,
                        height: '70%',
                        width: 800,
                        url: IFM_CONTEXT + Gikam.choose.getConfig("test-method").url + Gikam.param({
                            testId_EQ: activedRow.testId,
                            target_filter: encodeURI(JSON.stringify({
                                type: 'unassigned',
                                targetTable: 'T_LIMS_EXPERIMENTAL_TEMPLATE_TEST_METHOD',
                                targetMatchColumn: 'TESTMETHODID',
                                filter: [{
                                    targetFilterColumn: 'etTestId',
                                    targetFilterValue: activedRow.id
                                }]
                            }))
                        }),
                        onAfterClose: function (rows) {
                            if (!Gikam.isEmpty(rows)) {
                                var chooseData = rows.map(function (item) {
                                    return {
                                        etId: activedRow.etId,
                                        etTestId: activedRow.id,
                                        testMethodId: item.id,
                                        methodCode: item.ext$.methodcode,
                                        methodDesc: item.ext$.methoddesc,
                                        defaultMethod: item.defaultMethod
                                    }
                                });
                                Gikam.getComp('template-test-method-edit-list-grid').insert(limsExperimentalTemplate.etTestMethodUrl, chooseData).done(function () {
                                    Gikam.getComp('template-test-method-edit-list-grid').refresh();
                                });
                            }
                        }
                    })
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('template-test-method-edit-list-grid').deleteRows(limsExperimentalTemplate.etTestMethodUrl);
                }
            } ]
        }
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-experimental-template-edit-list-layout',
            renderTo : workspace.window.$dom,
            west: {
                width: '40%',
                title: 'T_LIMS_EXPERIMENTAL_TEMPLATE.SERVICE_NAME',
                items: [this.getGridParam()]
            },
            center: {
                items: [{
                    type: 'layout',
                    north: {
                        height: '60%',
                        title: 'T_LIMS_EXPERIMENTAL_TEMPLATE_TEST.SERVICE_NAME',
                        items: [this.getEtTestGridParam()]
                    },
                    center: {
                        title: 'T_LIMS_EXPERIMENTAL_TEMPLATE_TEST_METHOD.SERVICE_NAME',
                        items: [this.getSpMethodGridParam()]
                    }
                }]
            },
        });
    },

    init : function() {
        this.create();
    }
};
